package leetcode;

import java.util.*;
import java.util.Set;

public class LeetCodeMain111 {
    public int[] singleNumbers(int[] nums) {
        List<Integer> list=new ArrayList<>();
        Arrays.sort(nums);
        int length=nums.length;
        int num=nums[0];
        int count=1;
        while (count<length){
            num=num^nums[count];
            if (num!=0){
                num^=nums[count];
                list.add(num);
                count++;
            }else {
                count++;
                while (count<length-1&&nums[count]==nums[count++]){
                    count++;
                }
            }
        }
        int[] arr=new int[list.size()];
        for (int i = 0; i < arr.length; i++) {
            arr[i]=list.get(i);
        }
        return arr;
    }
    public List<String> res=new ArrayList<>();
    public StringBuffer buffer=new StringBuffer();
    public String[] permutation(String s) {
        char[] chars=s.toCharArray();
        Arrays.sort(chars);
        boolean[] booleans=new boolean[chars.length];
        back(chars,0,chars.length,booleans);
        String[] strings=new String[res.size()];
        for (int i = 0; i < res.size(); i++) {
            strings[i]=res.get(i);
        }
        return strings;
    }
    public void back(char[] arr,int startIndex,int length,boolean[] flag){
        if (buffer.length() == length) {
            res.add(buffer.toString());
            return;
        }
        for (int i = 0; i < length; i++) {
            if (i>0&&arr[i]==arr[i-1]&&flag[i-1]==false){
                continue;
            }
            if (!flag[i]){
                flag[i]=true;
                buffer.append(arr[i]);
                back(arr, startIndex, length, flag);
                flag[i]=false;
                buffer.deleteCharAt(buffer.length()-1);
            }
        }
    }
}
